<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="400px">
      <el-form :model="formData">
        <el-form-item label="地点名称" :label-width="formLabelWidth">
          <el-input v-model="formData.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="p-10">
      <el-button type="primary" @click="openSave">添加主节点</el-button>
    </div>
    
    <div class="p-10">
      <el-tree
        ref="tree"
        node-key="id"
        :data="dataList" 
        :props="{children: 'localeInfoList', label: 'name'}"
      >
        <div slot-scope="{ node, data }" class="w100 u-f-asc p-5">
          <div class="u-f-ajc">
            {{ node.label }}
          </div>
          <div>
            <el-button
              type="text"
              size="mini"
              @click.stop = "() => append(data)">
              插入
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click.stop = "() => update(data)">
              更新
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click.stop = "() => remove(node, data)">
              删除
            </el-button>
          </div>
        </div>
      </el-tree>
    </div>
  </div>
</template>

<script>
import {localeApiSave, localeList, localeApiDel} from '@/api/locale-api'
export default {
  data() {
    return {
      title: '',
      dialogVisible: false,

      params : {

      },
      dataList: [
      ],

      formLabelWidth: '80px',
      formData: {
        
      },
      submitModel: 'add',
      dataItem: {}
    }
  },
  created() {
    this.listData()
  },
  methods: {
    listData(){
      localeList(this.params).then(res => {
        if (res.code === '200') {
          this.dataList = res.data
        }
      })
    },
    loadNode(node, resolve) {
      const params = {}
      if (node.data.id) {
        params.localeId = node.data.id
      }
      localeList(params).then(res => {
        if (res.code === '200') {
          return resolve(res.data);
        }
      })
    },
    append(data) {
      this.submitModel = 'add'
      this.title = '插入节点'
      this.dialogVisible = true
      this.formData = {
        localeId: data.id
      }
      this.dataItem = data
    },
    update(data) {
      this.submitModel = 'update'
      this.title = '更新节点'
      this.dialogVisible = true
      this.formData = data
    },

    remove(node, data) {
      localeApiDel(data.id).then(res => {
        if (res.code === '200') {
          this.$message.success('操作成功')
          const parent = node.parent;
          const children = parent.data.localeInfoList || parent.data;
          const index = children.findIndex(d => d.id === data.id);
          children.splice(index, 1);
        }
      })
    },

    openSave(){
      this.submitModel = 'addMain'
      this.title = '添加主节点'
      this.dialogVisible = true
      this.formData = {}
    },
    submitForm(){
      localeApiSave(this.formData).then(res => {
        if (res.code === '200') {
          this.dialogVisible = false
          this.$message.success('操作成功')
          if (this.submitModel === 'addMain') {
            this.dataList.push(res.data)
          }else if (this.submitModel === 'add') {
            this.dataItem.localeInfoList.push(res.data)
          }else if (this.submitModel === 'update') {
            this.dataItem = res.data
          }
          
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>

</style>